package com.caesar.exer;

import lombok.Data;

/**
 * @author caesar
 * @date 2025/7/21
 * @Description:运动员类
 * @apiNote
 */
@Data
public class Runner extends  Thread{
    private static long distance;  	//全程距离，单位米//运动员名字
    private long runTime;   	//每米需要时间，单位毫秒
    private long restTime;  	//每10米的休息时间，单位毫秒
    private long time;      	//跑完全程的总时间
    private boolean finished; 	//是否跑完全程

    public Runner(String name, long runTime, long restTime) {
        super(name);
        this.runTime = runTime;
        this.restTime = restTime;
    }
    public void run() {
        long sum = 0;
        long start = System.currentTimeMillis();
        while (sum < distance) {
            try {
                Thread.sleep(runTime);// 每米距离，该运动员需要的时间
                sum++;
                System.out.println(getName() + "已经跑了" + sum + "米，继续努力奔跑...");
                if (sum % 10 == 0 && sum < distance) {
                    // 每10米休息一下
                    System.out.println(getName() + "已经跑了" + sum + "米，开始休息......");
                    Thread.sleep(restTime);
                }
            } catch (InterruptedException e) {
                System.out.println(getName()  + "出现了意外，退出了比赛");
                return;
            }
        }
        long end = System.currentTimeMillis();
        time = end - start;
        System.out.println(getName() + "跑了" + sum + "米，已到达终点，共用时" + (double) time / 1000.0 + "秒");
        finished = true;
    }
    public long getTime() {
        return time;
    }

    public boolean isFinished() {
        return finished;
    }

    public static void setDistance(long distance) {
        Runner.distance = distance;
    }
}
